﻿using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class CommandGenerator
    {
        public static void Generate(DatabaseTableSchema tbSchema)
        {
            var sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using Common.Commands;");
            sb.AppendLine("using Sanelib.Dto;");
            sb.AppendLine("");
            sb.AppendLine("namespace Common.Commands." + tbSchema.TableName + "Commands");
            sb.AppendLine("{");

            sb.AppendLine("\tpublic class Add" + tbSchema.TableName + ": ICommand");
            sb.AppendLine("\t{");
            sb.AppendLine("\t\tpublic Guid Id { get; set; }");

            foreach (var item in tbSchema.Columns)
            {
                sb.AppendLine(item.GetPropertyDeclaration("\t\t"));
            }
            
            sb.AppendLine("");
            sb.AppendLine("\t\tpublic Guid GetAggregateId()");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\treturn Id;");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");

            sb.AppendLine("\t\tpublic ErrorResult Validate()");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tvar validationResult = new ErrorResult();");
            sb.AppendLine("\t\t\treturn validationResult;");
            sb.AppendLine("\t\t}");

            sb.AppendLine("\t}");

            sb.AppendLine("");

            sb.AppendLine("\tpublic class Update" + tbSchema.TableName + ": Add" + tbSchema.TableName);
            sb.AppendLine("\t{");
            sb.AppendLine("");
            sb.AppendLine("\t}");

            sb.AppendLine("");
            
            sb.AppendLine("\tpublic class Delete" + tbSchema.TableName + ": ICommand");

            sb.AppendLine("\t{");
            sb.AppendLine("\t\tpublic Delete" + tbSchema.TableName + "(Guid id)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tId = id;");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");
            sb.AppendLine("\t\tpublic Guid Id { get; set; }");
            sb.AppendLine("");
            sb.AppendLine("\t\tpublic Guid GetAggregateId()");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\treturn Id;");
            sb.AppendLine("\t\t}");
            sb.AppendLine("");

            sb.AppendLine("\t\tpublic ErrorResult Validate()");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tvar validationResult = new ErrorResult();");
            sb.AppendLine("\t\t\treturn validationResult;");
            sb.AppendLine("\t\t}");

            sb.AppendLine("\t}");

            sb.AppendLine("}");

            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("..\\Common\\Commands\\") + tbSchema.TableName + "Commands_Generated.cs", sb.ToString());
        }
    }
}
